using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class RenameEditor : MonoBehaviour
{
	[MenuItem("Paperclips/Rename Tiles")]
	public static void RenameTiles()
	{
		Transform tileParent = GameObject.FindObjectOfType<Tile>().transform.parent;
		
		for (int i = 0; i < tileParent.childCount; i++)
		{
			Transform child = tileParent.GetChild(i);
			child.name = "Tile (" + ((int)child.position.x).ToString("d2") + ", " + ((int)child.position.y).ToString("d2") + ")";
		}

		OrderChildrenByName(tileParent);
	}

	private static void OrderChildrenByName(Transform _parent)
	{
		List<Transform> children = new List<Transform>();
		for (int i = 0; i < _parent.childCount; i++)
		{
			children.Add(_parent.GetChild(i));
		}

		children.Sort(delegate(Transform a, Transform b)
		{
			return string.Compare(a.name, b.name);
		});

		for (int i = 0; i < children.Count - 1; i++)
		{
			children[i].SetSiblingIndex(i);
		}
	}
}
